package cn.woblog.android.common.utils.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import cn.woblog.android.common.utils.IntentUtils;
import cn.woblog.android.common.utils.SPUtils;
import launcher.simple.android.woblog.cn.widget.AppView;


/**
 * Public fragment parent class,all the classes should be inherited.
 */
public abstract class BaseFragment extends Fragment {

	private SPUtils spUtils;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		beforeInit();
	}

	private void beforeInit() {
		spUtils = SPUtils.getInstance(getActivity().getApplicationContext());
	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = getLayoutView(inflater, container, savedInstanceState);
		return view;
	}

	/**
	 * set fragment layout
	 * @param inflater
	 * @param container
	 * @param savedInstanceState
	 * @return
	 */
	protected abstract View getLayoutView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		initViews();
		initStyles();
		initDatas();
		initListener();
		super.onViewCreated(view, savedInstanceState);
	}

	/**
	 * 找控件
	 */
	protected void initViews() {

	}

	/**
	 * 动态设置样式，颜色，宽高，背景
	 */
	protected void initStyles() {
	}

	/**
	 *设置数据
	 */
	protected void initDatas() {

	}

	/**
	 * 绑定监听器
	 */
	protected void initListener() {
	}

	//activity 条转方法
	public void toActivity(Class<?> clazz){
		IntentUtils.toActivity(getActivity(), clazz);
	}

	public void toActivityForResult(Intent intent, int requestCode) {
//		IntentUtils.toActivityForResut(getActivity(), intent, requestCode);
		startActivityForResult(intent,requestCode);
	}
	public void toActivityAfterFinishThis(Class<?> clazz){
		IntentUtils.toActivityAfterFinishThis(getActivity(), clazz);
	}
	public void toActivity(Intent intent){
		IntentUtils.toActivity(getActivity(), intent);
	}

	//\activity 条转方法

	public View findViewById(int id) {
		return getView().findViewById(id);
	}
}
